package com.platform.common.base;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础实体类（所有model都要继承它）
 * 
 * @author wangjun
 *
 */
public abstract class BaseModel extends PageModel implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 字符串转换成时间的格式
	 */
	public static String DATEFORMAT = "yyyy-MM-dd HH:mm";

	/**
	 * VO创建时间
	 */
	private Date createDate;

	/**
	 * VO最近修改时间
	 */
	private Date lastModifiedDate;

	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				// 过滤class属性
				if (!key.equals("class")) {
					// 得到property对应的getter方法
					Method getter = property.getReadMethod();
					Object value = getter.invoke(this);
					map.put(key, value);
				}

			}
		} catch (Exception e) {
			System.out.println("transBean2Map Error " + e);
		}
		return map;
	};

	public Object toVO(Map<String, Object> map) {
		if (map == null) {
			return null;
		}
		Object obj = null;
		try {
			obj = this.getClass().newInstance();
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				Method setter = property.getWriteMethod();
				if (setter != null) {
					setter.invoke(obj, map.get(property.getName()));
				}
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;

	}

	public Object toVO2(Map<String, Object> map) {
		if (map == null) {
			return null;
		}
		Object obj = null;
		try {
			obj = this.getClass().newInstance();
			org.apache.commons.beanutils.BeanUtils.populate(obj, map);
		} catch (InstantiationException e) {
			// TODO Auto-generateds catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return obj;
	}

	// 价格转换浮点型，保留两位小数,不足补零
	public BigDecimal toBigDecimalFormart(BigDecimal num) {
		if (num != null) {
			num = num.setScale(2, BigDecimal.ROUND_HALF_UP);// 设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
		}
		return num;
	}

	public Double toDoubleFormart(Double num) {
		if (num != null) {
			String str = String.valueOf((num * 100) / 100);
			String[] xsd = str.split(".");
			if (xsd.length == 1) {
				str = str + ".00";
			}
			if (xsd.length > 1) {
				if (xsd[1].length() < 2) {
					str = str + "0";
				}
			}
			num = Double.parseDouble(str);
		}

		return num;
	}

	public static String getDATEFORMAT() {
		return DATEFORMAT;
	}

	public static void setDATEFORMAT(String dATEFORMAT) {
		DATEFORMAT = dATEFORMAT;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getLastModifiedDate() {
		return lastModifiedDate;
	}

	public void setLastModifiedDate(Date lastModifiedDate) {
		this.lastModifiedDate = lastModifiedDate;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
}